﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Data.Entity.Migrations;
using System.Data.Entity.Validation;
using System.Linq;
using System.Web;
using MyLocalChurch.Constants;
using MyLocalChurch.Models;
using log4net;

namespace MyLocalChurch
{
    public class MyDbInitializer : DropCreateDatabaseIfModelChanges<MyContext>
    {
        private static readonly ILog _logger = LogManager.GetLogger(typeof(MyDbInitializer));

        protected override void Seed(MyContext context)
        {
            GetChurchServices().ForEach(cs => context.ChurchServices.AddOrUpdate(cs));

            var validationErrors = context.GetValidationErrors();
            foreach (var eve in validationErrors)
            {
                _logger.Error(string.Format(
                    "Entity of type \"{0}\" in state \"{1}\" has the following validation errors:",
                    eve.Entry.Entity.GetType().Name, eve.Entry.State));

                foreach (var ve in eve.ValidationErrors)
                {
                    _logger.Error(string.Format("- Property: \"{0}\", Error: \"{1}\"",
                                                ve.PropertyName, ve.ErrorMessage));
                }
            }
        }

        private static List<ChurchService> GetChurchServices()
        {
            var churchServices = new List<ChurchService>
                {
                    new ChurchService
                        {
                            Id = 1,
                            DayOfWeek = DayOfWeek.Sunday,
                            StartTime = "09:00",
                            CreatedBy = SystemConstants.System,
                            CreatedDate = DateTime.Now
                        },
                    new ChurchService
                        {
                            Id = 2,
                            DayOfWeek = DayOfWeek.Sunday,
                            StartTime = "11:00",
                            CreatedBy = SystemConstants.System,
                            CreatedDate = DateTime.Now
                        },
                    new ChurchService
                        {
                            Id = 3,
                            DayOfWeek = DayOfWeek.Sunday,
                            StartTime = "14:00",
                            CreatedBy = SystemConstants.System,
                            CreatedDate = DateTime.Now
                        },
                    new ChurchService
                        {
                            Id = 4,
                            DayOfWeek = DayOfWeek.Sunday,
                            StartTime = "16:00",
                            CreatedBy = SystemConstants.System,
                            CreatedDate = DateTime.Now
                        },
                    new ChurchService
                        {
                            Id = 5,
                            DayOfWeek = DayOfWeek.Sunday,
                            StartTime = "18:00",
                            CreatedBy = SystemConstants.System,
                            CreatedDate = DateTime.Now
                        },
                };

            return churchServices;
        }
    }
}